package com.cuxiaoyi.action;

import java.sql.Connection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

import com.cuxiaoyi.Constants;
import com.cuxiaoyi.db.DBUtils;
import com.cuxiaoyi.db.Dao;
import com.cuxiaoyi.taobao.TaobaoProxy;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taobao.api.ApiException;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.domain.Promotion;
import com.taobao.api.request.MarketingPromotionAddRequest;
import com.taobao.api.request.MarketingPromotionUpdateRequest;
import com.taobao.api.response.MarketingPromotionUpdateResponse;

public class UpdatePromotionAction extends ActionSupport implements Constants{

	static Logger _log = Logger.getLogger(UpdatePromotionAction.class.getName());
	
	private MarketingPromotionAddRequest promotionAddRequest;
	
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String sessionId = (String)session.get(TOP_SESSION);
		String numIids = promotionAddRequest.getNumIids();
		String[] ids = StringUtils.split(numIids, ',');
		Connection conn = null;
		try
		{
			conn = DBUtils.getConnection();
			for (String numIid : ids)
			{
				Promotion p = Dao.INSTANCE.getPromotion(Long.parseLong(numIid), conn);
				_log.info("num_iid: " + numIid + ", updating promotion: " + p);
				if (p != null)
				{
					try
					{
						updatePromotion(p, sessionId, conn);
					}
					catch (Exception e)
					{
						_log.log(Level.SEVERE, "", e);
					}
				}
			}
		}
		catch (Exception e)
		{
			_log.log(Level.SEVERE, "", e);
		}
		finally
		{
			DBUtils.close(conn);
		}
		return SUCCESS;
	}
	
	private void updatePromotion(Promotion p, String sessionId, Connection conn) throws ApiException
	{
		MarketingPromotionUpdateRequest req = new MarketingPromotionUpdateRequest();
		req.setPromotionId(p.getPromotionId());
		req.setTagId(promotionAddRequest.getTagId());
		req.setDiscountType(promotionAddRequest.getDiscountType());
		req.setDiscountValue(promotionAddRequest.getDiscountValue());
		req.setStartDate(promotionAddRequest.getStartDate());
		req.setEndDate(promotionAddRequest.getEndDate());
		req.setPromotionTitle(promotionAddRequest.getPromotionTitle());
		req.setPromotionDesc(promotionAddRequest.getPromotionDesc());
		MarketingPromotionUpdateResponse rsp = TaobaoProxy.getClient().execute(req, sessionId);
		if (rsp.isSuccess())
		{
			_log.info("update promotion successfully. ");
			try
			{
				p.setTagId(promotionAddRequest.getTagId());
				p.setDiscountType(promotionAddRequest.getDiscountType());
				p.setDiscountValue(promotionAddRequest.getDiscountValue());
				p.setStartDate(promotionAddRequest.getStartDate());
				p.setEndDate(promotionAddRequest.getEndDate());
				p.setPromotionTitle(promotionAddRequest.getPromotionTitle());
				p.setPromotionDesc(promotionAddRequest.getPromotionDesc());
				Dao.INSTANCE.updatePromotion(p, conn);
			}
			catch (Exception e)
			{
				_log.log(Level.SEVERE, "", e);
			}
		}
		else
		{
			error(rsp);
		}
	}
	
	public void setPromotionAddRequest(MarketingPromotionAddRequest promotionAddRequest) {
		this.promotionAddRequest = promotionAddRequest;
	}

	public MarketingPromotionAddRequest getPromotionAddRequest() {
		return promotionAddRequest;
	}
	
	private void error(TaobaoResponse rsp)
	{
		_log.info(rsp.getErrorCode() + " - " + rsp.getMsg() + " - " + rsp.getSubCode() + " - " + rsp.getSubMsg());
	}
}
